package com.example.demo.service.impl;

import com.example.demo.dao.Comment;
import com.example.demo.dao.CommentExample;
import com.example.demo.dao.HuiFu;
import com.example.demo.mapper.CommentMapper;
import com.example.demo.mapper.HuiFuMapper;
import com.example.demo.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private HuiFuMapper huiFuMapper;
    //通过商品的id 查找评论
    @Override
    public List<Comment> findcomments(Integer id) {
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria com = commentExample.createCriteria();
        com.andGoodsidEqualTo(id);
        return  commentMapper.selectByExample(commentExample);
    }
 //查看评论及回复
    @Override
    public List<Comment> findAll() {

        List<Comment> commentAll = commentMapper.findCommentAll();
        for(Comment comment : commentAll){
            List<HuiFu> huifus = huiFuMapper.findCommentByid(comment.getCommentid());
            comment.setHuiFus(huifus);
        }
        return commentAll;
    }

    //添加评论
    @Override
    public int addcomment(Comment comment) {
        return  commentMapper.insert(comment);
    }


}
